package org.linuxgears.BouncingBall.model;

import java.awt.*;

import org.linuxgears.BouncingBall.model.context.BallContext;
import org.linuxgears.BouncingBall.mover.strategy.ControlledMovingStrategy;
import org.linuxgears.BouncingBall.mover.strategy.PausableMovingStrategy;

/**
 * Created by IntelliJ IDEA.
 * User: khilkeol
 * Date: 31 ���� 2010
 * Time: 9:47:18
 * To change this template use File | Settings | File Templates.
 */
public class BallController {
    private BouncingBallModel model;

    public BallController(BouncingBallModel model) {
        this.model = model;
    }

    public void addBall() {
        this.addBall(BallFactory.getInstance().createColoredBall());
    }

    public void addBall(Point position) {
        this.addBall(BallFactory.getInstance().createColoredBall(), position);
    }

    public void addBall(Ball ball) {
        this.addBall(ball, (Point)BouncingBallModel.DEFAULT_POSITION.clone());
    }

    public void addBall(Ball ball, Point position) {
        model.addBall(ball, position);
    }

    public void selectNextBall() {
        model.selectNextBall();
    }

    public void selectPreviousBall() {
        model.selectPreviousBall();
    }

    public void deselectBall() {
        model.selectBall(null);
    }

    public void togglePause() {
        model.toggle();
    }

	public void resetState() {
		model.resetState();
	}

	public void deleteSelectedBall() {
		model.removeBall(model.getActiveBall());
	}

	public void addInteractiveBall(Ball ball) {
		model.addBall(ball, (Point)BouncingBallModel.DEFAULT_POSITION.clone(), new PausableMovingStrategy(new ControlledMovingStrategy()));
		model.getBallContext(ball).getMover().getMovingStrategy().setTargetPoint(new Point(200, 50));
	}

	public void selectBallAt(Point position) {
		model.selectBallAt(position);
	}

	public void moveSelectedBallTo(Point newPosition) {
		BallContext activeContext = model.getBallContext(model.getActiveBall());
		if (activeContext != null && activeContext.getBallRunnable() != null) {
			model.moveSelectedBallTo(newPosition);
		};
	}
}
